using System;
using CodeStory.Core;
using CodeStory.Core.ObjectEntity;
using CodeStory.Core.UI;
using System.Web.UI;
using System.Collections;
using CodeStory.WebControls;
using CodeStory.Core.Attributes;

namespace CodeStory.WebApplication.Modules.Core.Admin
{
	[NavigationPortlet("CodeStory_Core_Admin_PortletSetting")]
	public partial class PortletSetting : Portlet
	{
		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(PortletSetting_Load);
			btnSaveSetting.Click += new EventHandler(ButtonSaveSetting_Click);
			//propertyGrid1.BeforeGenereateObject += new EventHandler(SettingGrid_BeforeGenereateObject);
			propertyGrid1.BeforeGenereateObject += new CodeStory.WebControls.PropertyGridEventHandler(SettingGrid_BeforeGenereateObject);
			propertyGrid1.CreatingItem += new PropertyGridCreateItemEventHandler(SettingGrid_CreatingItem);
			base.OnInit(e);
		}

		void SettingGrid_CreatingItem(object sender, PropertyGridCreateItemEventArgs e) {
			Attribute attr = e.PropertyDescripor.Attributes[typeof(PortletSettingAttribute)];
			if (attr == null) {
				e.Cancel = true;
			}
		}

		void SettingGrid_BeforeGenereateObject(object sender, PropertyGridEventArgs e) {
			IPortlet m = e.SelectedObject as IPortlet;
			if (m != null) {
				m.TrackSetting();
			}
		}

		private void ButtonSaveSetting_Click(object sender, EventArgs e) {
			object obj = propertyGrid1.SelectedObject;
			IPortlet manager = obj as IPortlet;
			if (manager == null)return;
			ILayoutPage layout = Page as ILayoutPage;
			if (layout == null) return;
			string id = Request.QueryString["pageportletid"];
			Guid guid = new Guid(id);
			PagePortletObject pageportlet = PagePortletObject.Read(guid);
			Control control = layout.Builder.LoadPortletControl(pageportlet.PortletObject);
			IDictionary dict = layout.Builder.LoadSettings(pageportlet.PageObject.PageSetting);
			dict[guid] = manager.SaveSetting();

			PageObject page = pageportlet.PageObject;
			ObjectStateFormatter f = new ObjectStateFormatter();
			page.PageSetting = f.Serialize(dict);
			page.Save();
		}

		private void PortletSetting_Load(object sender, EventArgs e) {
			if (EditMode) {
				return;
			}
			string pageportletid = Request.QueryString["pageportletid"];
			if (string.IsNullOrEmpty(pageportletid) && !EditMode) {
				throw new NeedQueryStringException("pageportletid");
			}
			if (!IsPostBack) {
				BindPagePortletSetting(new Guid(pageportletid));
			}
		}

		private void BindPagePortletSetting(Guid guid) {
			ILayoutPage layout = Page as ILayoutPage;
			if (layout == null) return;

			PagePortletObject pageportlet = PagePortletObject.Read(guid);
			Control control = layout.Builder.LoadPortletControl(pageportlet.PortletObject);
			IDictionary dict = layout.Builder.LoadSettings(pageportlet.PageObject.PageSetting);
			if (dict != null && dict.Contains(guid)) {
				object setting = dict[guid];
				if (setting != null) {
					IPortlet m = control as IPortlet;
					if (m != null) {
						m.LoadSetting(setting);
					}
				}
			}
			propertyGrid1.SelectedObject = control;
			propertyGrid1.DataBind();
		}
	}
}